
package com.ossjk.config.knife4j;

import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import com.ossjk.core.constant.Constant;

import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@ConditionalOnProperty(value = { "knife4j.enable" }, matchIfMissing = true)
public class SwaggerConfiguration {

	@Bean(value = "defaultApi")
	public Docket defaultApi() {
//		ParameterBuilder parameterBuilder = new ParameterBuilder();
//		List<Parameter> parameters = Lists.newArrayList();
//		parameterBuilder.name("token").description("token令牌").modelRef(new ModelRef("String")).parameterType("header").required(true).build();
//		parameters.add(parameterBuilder.build());

		Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("默认接口").select().apis(RequestHandlerSelectors.basePackage("com.ossjk.qlh"))
				// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any()).build().securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
		return docket;
	}

	private SecurityContext securityContext() {
		return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("/.*")).build();
	}

	private ApiKey apiKey() {
		return new ApiKey(Constant.REQUEST_HEADER_TOKEN, Constant.REQUEST_HEADER_TOKEN, "header");
	}

	List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return Lists.newArrayList(new SecurityReference(Constant.REQUEST_HEADER_TOKEN, authorizationScopes));
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("qlh-api").description("qlh接口").termsOfServiceUrl("http://localhost:8999/").contact("developer@mail.com").version("1.0").build();
	}

}
